<template>
    <div class="lib-create-template">
        <el-button class="return" type="text" @click="jumpToDefault"><i class="fa fa-angle-left mr5"></i>返回</el-button>
        <h4 class="header">创建资料信息</h4>
        <div class="content">
            <div class="filter-section">
                <span class="fl title">员工基本信息</span>
                <span class="fr gray">注：带有*为必填项</span>
            </div>
            <div class="lib-form">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
                    <el-form-item label="资料名称" prop="name">
                        <el-input v-model="ruleForm.name" size="mini"></el-input>
                    </el-form-item>
                    <el-form-item label="资料分类" prop="category">
                        <el-select v-model="ruleForm.category" placeholder="请选择资料分类" size="mini">
                            <el-option label="区域一" value="shanghai"></el-option>
                            <el-option label="区域二" value="beijing"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="文件类型" prop="fileType">
                        <el-select v-model="ruleForm.fileType" placeholder="请选择文件类型" size="mini">
                            <el-option label="区域一" value="shanghai"></el-option>
                            <el-option label="区域二" value="beijing"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="上传文件" prop="files">
                        <el-upload
                                class="upload-file"
                                action="https://jsonplaceholder.typicode.com/posts/"
                                :on-preview="handlePreview"
                                :on-remove="handleRemove"
                                :before-remove="beforeRemove"
                                multiple
                                :limit="3"
                                :on-exceed="handleExceed"
                                :file-list="ruleForm.fileList">
                            <el-button size="mini" type="primary">点击上传</el-button>
                            <div slot="tip" class="el-upload__tip fr">只能上传.xlsx文件，且不超过500M</div>
                        </el-upload>
                    </el-form-item>
                    <el-form-item label="备注" prop="note" class="form-textarea">
                        <el-input type="textarea" v-model="ruleForm.note" size="mini"></el-input>
                    </el-form-item>
                    <el-form-item label="发布者" prop="publisher">
                        <el-input v-model="ruleForm.publisher" size="mini"></el-input>
                    </el-form-item>
                    <div class="btn-wrap">
                        <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
                        <el-button @click="cancelForm()">取消</el-button>
                    </div>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "LibCreate",
        data() {
            return {
                ruleForm: {
                    name: "",
                    category: "",
                    fileType: "",
                    fileList: [{
                        name: 'food.xlsx',
                        url: ''
                    }, {
                        name: 'food2.xlsx',
                        url: ''
                    }],
                    note: "",
                    publisher: ""
                },
                rules: {
                    name: [
                        {required: true, message: '请输入资料名称', trigger: 'blur'}
                    ],
                    category: [
                        {required: true, message: '请选择资料分类', trigger: 'change'},
                    ],
                    fileType: [
                        {required: true, message: '请选择文件类型', trigger: 'change'}
                    ],
                    fileList: [
                        {required: true, message: '请上传文件', trigger: 'change'}
                    ],
                    publisher: [
                        {required: true, message: '请输入发布者', trigger: 'blur'},
                    ]
                }
            }
        },
        methods: {
            handleRemove(file, fileList) {
                console.log(file, fileList);
            },
            handlePreview(file) {
                console.log(file);
            },
            handleExceed(files, fileList) {
                this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
            },
            beforeRemove(file, fileList) {
                return this.$confirm(`确定移除 ${file.name}？`);
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        console.log('submit!');
                        this.jumpToDefault();
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            cancelForm() {
                this.jumpToDefault();
            },
            jumpToDefault() {
                this.$parent.curPath = 'default';
            }
        }
    }
</script>

<style lang="scss">
    @import "src/common/common.style";
    .lib-create-template {
        background-color: #ffffff;
        width: 100%;
        height: 100%;
        position: relative;
        .return {
            position: absolute;
            top: 0;
            left: 15px;
            padding: 0;
        }
        .header {
            text-align: center;
        }
        .content {
            height: 100%;
            .filter-section {
                border-bottom: 1px solid #ddd;
                padding-bottom: 25px;
                margin: 0 10px;
                padding-left: 5px;
                padding-right: 5px;
                .title {
                    font-size: $font-mn;
                    font-weight: 600;
                }
                .gray {
                    color: gray;
                }
            }
            .lib-form {
                height: 100%;
                padding-top: 10px;
                .el-form {
                    height: calc(100% - 120px);
                    @include ofy-a();
                    margin: 0 50px;
                    .el-form-item:not(.form-textarea) {
                        margin-bottom: 15px;
                        .el-form-item__content {
                            @include fl();
                            margin-left: 0 !important;
                        }
                        .el-input {
                            width: 300px;
                        }
                    }
                    .form-textarea {
                        .el-textarea {
                            width: 80%;
                            float: left;
                            .el-textarea__inner {
                                min-height: 100px !important;
                            }
                        }
                    }
                    .upload-file {
                        .el-upload-list__item:first-child{
                            margin-top: 0;
                        }
                        .el-upload-list__item-name{
                            text-align: start;
                        }
                        .el-upload__tip {
                            margin-top: 0;
                            margin-left: 5px;
                        }
                        .el-button {
                            margin: 0;
                        }
                    }
                    .btn-wrap {
                        margin-top: 50px;
                        .el-button {
                            width: 100px;
                            margin: 0 50px;
                        }
                    }
                }
            }
        }
    }
</style>